package com.wisdom.tools;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.StrictMode;
 
/**
 * HttpUtil Class Capsule Most Functions of Http Operations
 * 
 * @author sfshine
 * 
 */ 
public class HttpUtil { 
	private static final String APPLICATION_JSON = "application/json";  
    
    private static final String CONTENT_TYPE_TEXT_JSON = "text/json";  
    public static JSONObject post(String url,JSONObject json){
    	HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		JSONObject response = null;
		StreamToString test = new StreamToString();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

		StrictMode.setThreadPolicy(policy); 
		try {
			
	        post.addHeader("Content-Type", "application/json"); 
			post.addHeader("Accept","application/json");
			StringEntity s = new StringEntity(json.toString());
			s.setContentEncoding("UTF-8");
			s.setContentType("application/json");
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);
				 response =new JSONObject(result);  
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return response;
    	
    }
    
} 
